package com.ruoyi.business.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.business.vo.TWorkInfoVo;
import com.ruoyi.business.service.ITWorkInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.core.page.TableDataInfo;

import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;

/**
 * 常见问题Controller
 *
 * @author Tellsea
 * @date 2024-01-17
 */
@Api(tags = "常见问题Controller")
@RestController
@RequestMapping("/business/workInfo")
@RequiredArgsConstructor(onConstructor_ = @Autowired)
public class TWorkInfoController extends BaseController {

    private final ITWorkInfoService tWorkInfoService;

    @ApiOperation("查询常见问题列表")
    //@PreAuthorize("@ss.hasPermi('business:workInfo:list')")
    @GetMapping("/list")
    public TableDataInfo<TWorkInfoVo> list(TWorkInfoVo entity) {
        return tWorkInfoService.queryList(entity);
    }

    @ApiOperation("查询常见问题所有列表")
    @GetMapping("/listAll")
    public AjaxResult listAll(TWorkInfoVo entity) {
        return AjaxResult.success("查询成功", tWorkInfoService.queryAll(entity));
    }

    @ApiOperation("导出常见问题列表")
    @PreAuthorize("@ss.hasPermi('business:workInfo:export')")
    @Log(title = "常见问题", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TWorkInfoVo entity) {
        List<TWorkInfoVo> list = tWorkInfoService.queryAll(entity);
        ExcelUtil<TWorkInfoVo> util = new ExcelUtil<>(TWorkInfoVo.class);
        util.exportExcel(response, list, "常见问题数据");
    }

    @ApiOperation("获取常见问题详细信息")
    //@PreAuthorize("@ss.hasPermi('business:workInfo:query')")
    @GetMapping(value = "/getInfo/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        TWorkInfoVo entity = tWorkInfoService.queryById(id);
        if (Objects.isNull(entity.getReadNumber())) {
            entity.setReadNumber(0L);
        } else {
            entity.setReadNumber(entity.getReadNumber() + 1);
        }
        tWorkInfoService.updateById(entity);
        return AjaxResult.success("查询成功", entity);
    }

    @ApiOperation("新增常见问题")
    @PreAuthorize("@ss.hasPermi('business:workInfo:add')")
    @Log(title = "常见问题", businessType = BusinessType.INSERT)
    @PostMapping("add")
    public AjaxResult add(@RequestBody TWorkInfoVo entity) {
        return toAjax(tWorkInfoService.save(entity));
    }

    @ApiOperation("修改常见问题")
    @PreAuthorize("@ss.hasPermi('business:workInfo:edit')")
    @Log(title = "常见问题", businessType = BusinessType.UPDATE)
    @PostMapping("edit")
    public AjaxResult edit(@RequestBody TWorkInfoVo entity) {
        return toAjax(tWorkInfoService.updateById(entity));
    }

    @ApiOperation("删除常见问题")
    @PreAuthorize("@ss.hasPermi('business:workInfo:remove')")
    @Log(title = "常见问题", businessType = BusinessType.DELETE)
	@GetMapping("/remove/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(tWorkInfoService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }
}
